//
//  ERSettingMainViewController.m
//  ExchangeRate
//
//  Created by lanou on 15/11/14.
//  Copyright © 2015年 方义强. All rights reserved.
//

#import "ERSettingMainViewController.h"
#import "ERSettingSkillViewController.h"
#import "UMSocial.h"
#import "ERAboutUsViewController.h"

#define kCell @"settingCell"
@interface ERSettingMainViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate, UMSocialUIDelegate>
{
    int cellTag;
}
@property (nonatomic, strong) UITableView *mainTV;
@property (nonatomic, strong) NSArray *titleArr;
@end

@implementation ERSettingMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.072 green:0.075 blue:0.075 alpha:1.000];
    _topView.frame = CGRectMake(0, 0, kSCREENWIDTH, 90);
    _lineView.frame = CGRectMake(0, 89, kSCREENWIDTH, 1);
    _leftButton.frame = CGRectMake(10, 20, 50, 50);
    _titleLabel.frame = CGRectMake(kSCREENWIDTH / 2 - 100, 30, 200, 30);
    cellTag = 0;
    _titleArr = @[@[NSLocalizedString(@"换算小技巧", nil)], @[NSLocalizedString(@"点赞", nil), NSLocalizedString(@"分享给好友", nil)], @[NSLocalizedString(@"意见反馈", nil), NSLocalizedString(@"关于我们", nil)]];
    [self rewriteTopView];
    [self createTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 将状态栏改成白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark 重写topView
- (void) rewriteTopView
{
    _leftButton.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 30);
    [_leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(clickBackButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.titleLabel.text = NSLocalizedString(@"小工具", nil);
}

#pragma mark 点击返回按钮实现事件
- (void) clickBackButton:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableview 的初始化
- (void) createTableView
{
    _mainTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, kSCREENWIDTH, kSCREENHEIGHT - 70) style:(UITableViewStylePlain)];
    _mainTV.delegate = self;
    _mainTV.dataSource = self;
    _mainTV.backgroundColor = [UIColor colorWithWhite:0.213 alpha:1.000];
    [_mainTV registerClass:[UITableViewCell class] forCellReuseIdentifier:kCell];
    _mainTV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _mainTV.separatorColor = [UIColor clearColor];
    _mainTV.scrollEnabled = NO;
    [self.view addSubview:_mainTV];
}

#pragma mark -----------tableView 代理----------------
 #pragma mark 返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

#pragma mark 返回分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else return 2;
}

#pragma mark cell 重用 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    cell.textLabel.text = _titleArr[indexPath.section][indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.tag = kSettingCellTag + cellTag;
    cellTag ++;
    cell.backgroundColor = [UIColor colorWithWhite:0.249 alpha:1.000];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark 返回分区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

#pragma mark 自定义分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
    headerView.backgroundColor = [UIColor colorWithWhite:0.213 alpha:1.000];
    return headerView;
}

#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self clickCell:cell];
}

#pragma mark cell 的点击事件
- (void) clickCell:(UITableViewCell *)cell
{
    switch (cell.tag - kSettingCellTag) {
        case 0:
        {
            // 小技巧
            ERSettingSkillViewController *skillVC = [[ERSettingSkillViewController alloc] init];
            [self presentViewController:skillVC animated:YES completion:^{
                
            }];
            break;
        }
        
        case 1:
        {
            // 点赞
            [self showAlertWithTitle:@"暂未实现" andMessage:@"请期待下一个版本"];
            break;
        }
            
        case 2:
        {
            // 分享给好友
            [self shareForUMeng];
            break;
        }
            
        case 3:
        {
            // 意见反馈
            [self showAlertWithTitle:@"联系方式" andMessage:@"www.baidu.com"];
            break;
        }
            
        case 4:
        {
            // 关于我们
            ERAboutUsViewController *aboutVC = [[ERAboutUsViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:YES];
            break;
        }
    }
}

#pragma mark alert展示
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
    [alertView show];
}

#pragma mark UMeng分享
- (void) shareForUMeng
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5646e9e8e0f55ae4db000108"
                                      shareText:@"这款软件使用方便，你也试试"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToRenren,UMShareToWechatTimeline,
                                            UMShareToWechatSession,nil]
                                       delegate:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
